﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Payton.WP7.Common.Interface;
using Newtonsoft.Json.Linq;
using Payton.WP7.Common.Model;
using System.Globalization;
using System.Diagnostics;

namespace Payton.WP7.Common.Tencent
{
    public class TencentJTokenToStatusMapping : MappingBase<JToken, SNSStatus>
    {
        public TencentJTokenToStatusMapping(IMappingFactory factory)
            : base(factory)
        {

        }

        public override SNSStatus Map(JToken s)
        {
            try
            {
#if DEBUG
                Debug.WriteLine(s);
#endif
                if (s == null || !s.HasValues)
                    return null;
                var status = new SNSStatus()
                {
                    Account = this._factory.Account,
                    Provider = SNSProviderType.Tencent
                };

                if (s.SelectToken("id") != null)
                    status.Id = Int64.Parse((string)s.SelectToken("id"));

                if (s.SelectToken("count") != null)
                {
                    status.RepostCount = (int)s.SelectToken("count");
                }

                if (s.SelectToken("mcount") != null)
                {
                    status.CommentCount = (int)s.SelectToken("mcount");
                }


                //type
                if (s.SelectToken("time") != null)
                {
                    status.CreatedTime = DateTime.ParseExact("1970-01-01 08:00:00", "yyyy-MM-dd hh:mm:ss", new CultureInfo("en-us")).Add(TimeSpan.FromSeconds((long)s.SelectToken("time")));
                    status.Timestamp = (long)s.SelectToken("time");
                    status.Source = "Payton";
                    status.User = _factory.Account.UserLite;
                    status.Type = StatusType.Comment;
                    goto LABEL_RETURN;
                }
                else if (s.SelectToken("source") != null)
                    status.Type = StatusType.Repost;
                else
                    status.Type = StatusType.Original;

                if (s.SelectToken("timestamp") != null)
                {
                    status.CreatedTime = DateTime.ParseExact("1970-01-01 08:00:00", "yyyy-MM-dd hh:mm:ss", new CultureInfo("en-us")).Add(TimeSpan.FromSeconds((long)s.SelectToken("timestamp")));
                    status.Timestamp = (long)s.SelectToken("timestamp");
                }

                status.Text = s.SelectToken("origtext") != null ? HttpUtility.HtmlDecode((string)s.SelectToken("origtext")) : string.Empty;

                status.Source = s.SelectToken("from") == null ? "Payton" : (string)s.SelectToken("from");

                if (s.SelectToken("source") != null && s.SelectToken("source").HasValues)
                {
                    status.RetweetedStatus = _factory.GetMapping<JToken, SNSStatus>().Map(s.SelectToken("source"));
                    status.Prefix = AppResx.RepostPrifix;
                }
                if (s.SelectToken("image") != null && s.SelectToken("image").HasValues)
                {
                    status.ThumbnailPic = ((string)s.SelectToken("image").First + "/160").Replace("app.qpic.cn", "t1.qpic.cn");
                    status.BmiddlePic = ((string)s.SelectToken("image").First + "/460").Replace("app.qpic.cn", "t1.qpic.cn");
                }
                status.User = s.SelectToken("name") != null ? _factory.GetMapping<JToken, SNSUser>().Map(s) : _factory.Account.UserLite;


                //logic adding by Payton

                if (string.IsNullOrEmpty(status.Text) && status.RetweetedStatus != null)
                {
                    status.Text =  AppResx.RepostPrifix; ;
                }

            LABEL_RETURN:
                return status;

            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
                throw ex;
            }
        }

    }
}
